<%#
LuCI - Lua Configuration Interface
Copyright 2008 Steven Barth <steven@midlink.org>
Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

$Id$

-%>

<%
    local ubus = require "luci.ubus"
    local bit = require "nixio"
    local portNum = ubus.getPortNum()
    local unique = ubus.getUniqueId()
    -- 端口配置应用处理
    local modeJson = luci.http.formvalue("setMode")
    if modeJson then
        local setJson = luci.json.decode(modeJson)
        local setSnd = {}
        local setStatus = {}
        for i=1,portNum do
            local pVal = string.format("port-%d",i)
            if setJson.data[i].admin_mode == "1" then
                setSnd[i]={[pVal]={{["comb_type"] = setJson.data[i].comb_type},{["admin_mode"] = setJson.data[i].admin_mode},{["physical_mode"] = setJson.data[i].physical_mode},{["flow_control_mode"] = setJson.data[i].flow_control_mode}}} 
            else
                setSnd[i]={[pVal]={{["comb_type"] = setJson.data[i].comb_type},{["admin_mode"] = setJson.data[i].admin_mode}}} 
            end
        end
        setStatus = ubus.set_args("port_config", "port_config_set",setSnd,1)
        
        luci.http.prepare_content("application/json")
        luci.http.write_json(setStatus)
        return
    end
    -- 读取页面内容 

    local para = {"type","operational_status","physical_status","dot1d_state","admin_mode","physical_mode","flow_control_mode","comb_mode","comb_type"}
    local snd = {}
    for i=1,portNum do
        local pa = string.format("port-%d",i)
        snd[i]={[pa] = para}
    end
    
    local getStatus = {}
    getStatus = ubus.get_args("port_config", "port_config_get",snd,1)

    --- 如果是定制版本 获取link状态信息
    local fpga_rv = {}
    if unique == "54-10-01-06-01-01-00-00" or unique == "54-20-01-06-01-02-00-00"  then
        local val1 = luci.sys.exec('i2c_read 0x40 0x04')         
        local val2 = luci.sys.exec('i2c_read 0x40 0x07') 
        local val3 = luci.sys.exec('i2c_read 0x40 0x02')         
        local val4 = luci.sys.exec('i2c_read 0x40 0x03') 

        fpga_rv = {
            fpgaChannel2 = bit.bit.band(tonumber(string.sub(val1,-5,-1)), 0x01),
            fpgaVideo    = bit.bit.band(tonumber(string.sub(val2,-5,-1)), 0x01),
            fpgaChannel0 = bit.bit.band(tonumber(string.sub(val3,-5,-1)), 0x01),
            fpgaChannel1 = bit.bit.band(tonumber(string.sub(val4,-5,-1)), 0x01)
        }
    end
%>

<%+header%>

<script type="text/javascript" src="<%=resource%>/cbi.js"></script>
<script type="text/javascript">
    
    //获得端口配置表单内容转化为json格式并下发后台
    
    function setPort(){
        var a = JSON.parse("{\"data\":[]}");
        //将表格中内容转化为json格式
        $("#setTable tbody tr").each(function(i,v){
            var para = new Object();
            var iNum = i+1;
            para.comb_type = $("[name=combType"+iNum+"] option:selected").val();
            para.admin_mode = $("[name=adminMode"+iNum+"] option:selected").val();
            para.physical_mode = $("[name=physicalMode"+iNum+"] option:selected").val();
            para.flow_control_mode = $("[name=FlowControlMode"+iNum+"] option:selected").val();
            a.data.push(para);
        });
        //console.log(JSON.stringify(a));
        //异步提交数据
        XHR.get('<%=REQUEST_URI%>',{"setMode": JSON.stringify(a)}, 
                function(x,rv) {
                    var flag = 0;
                    for(var i = 0; i < rv.length; i++){
                        if(rv[i].ret == "-1"){
                            flag = 1;
                            alert("<%:NO.%>"+(i+1)+"<%:configuration failure%>");
                            return ;
                        }
                    }
                    if(flag == 0){
                        alert("<%:Succesfully configured%>");
                    }
                    window.location.reload();
                }
               );
    }
    $(document).ready(function(){
        //为每个管理模式绑定判断是否使能
        $("[name^=adminMode]").each(function(){
            $(this).on("change",function(){
                var num = this.name.replace(/[^0-9]/ig,"");
                checkMode(this.selectedIndex,num);
            });
        });
        $("[name^=combType]").each(function(){
            $(this).on("change",function(){
                var num = this.name.replace(/[^0-9]/ig,"");
                checkType($("[name=combType"+num+"] option:selected").val(),num);
            });
        });
        //在页面上显示后台传来的数据
        var modeObj = ["<%:Enable%>","<%:Disable%>"];
        var opeObj  = ["up","down","testing"];
        var typeObj = ["FE","FX","GE","GX","COMB"];
        var phyObj  = ["invilid","<%:10M-HALF%>","<%:10M-FULL%>","<%:100M-HALF%>","<%:100M-FULL%>","<%:1000M-FULL%>"];
        var dotObj  = ["disabled","blocked","listening","learning","forwarding"];
        var rcv = <%=luci.json.encode(getStatus)%>;
        $.each(rcv, function(i,val) {
            var rcv = val.ret[0]["port-"+(i+1)];
            var rowNum = i+1;

            var porType = typeObj[rcv[0].type-1];
     //       $("#setTable tr:eq("+rowNum+") td:nth-child(2)").text(porType);
            $("#showTable tr:eq("+rowNum+") td:nth-child(2)").text(porType);
            $("[name=combType"+rowNum+"]").val(rcv[8].comb_type);

            //非comb口按原来的机制分配
            if(rcv[7].comb_mode == 0){
                $("[name=combType"+rowNum+"]").attr("disabled","disabled");
                $("[name=physicalMode"+rowNum+"]")[0].remove(8);
                $("[name=physicalMode"+rowNum+"]")[0].remove(7);
                if ((porType=="FE")) { //如果为百兆电口 速率双工配置无千兆选项
                    $("[name=physicalMode"+rowNum+"]")[0].remove(6);
                }
                else if(porType=="FX"){//百兆光口
                    document.getElementById("port"+(i+1)).innerHTML = "F"+(i+1);
                    document.getElementById("portId"+(i+1)).innerHTML = "F"+(i+1); 
                    $("[name=physicalMode"+rowNum+"]").each(function(){
                        while(this.length>4){
                            this.remove(2);
                        }
                        this.remove(3);
                    });
                }
                else if (porType=="GX") { //如果为千兆光口 速率双工配置只有千兆选项与自协商
                    document.getElementById("port"+(i+1)).innerHTML = "G"+(i+1);
                    document.getElementById("portId"+(i+1)).innerHTML = "G"+(i+1);

                    $("[name=physicalMode"+rowNum+"]").each(function(){
                        while(this.length>3){
                            this.remove(2);
                        }
                    });
                }
            }
            //comb复用口
            else if(rcv[7].comb_mode == 1){
                $("[name=combType"+rowNum+"]")[0].remove(3);
                //$("[name=combType"+rowNum+"]")[0].remove(2);
                checkType(rcv[8].comb_type,rowNum);
                if(porType=="FX"){//百兆光口
                    document.getElementById("port"+(i+1)).innerHTML = "F"+(i+1);
                    document.getElementById("portId"+(i+1)).innerHTML = "F"+(i+1); 
                }
                else if (porType=="GX") { //如果为千兆光口 速率双工配置只有千兆选项与自协商
                    document.getElementById("port"+(i+1)).innerHTML = "G"+(i+1);
                    document.getElementById("portId"+(i+1)).innerHTML = "G"+(i+1);
                }
                else if (porType=="COMB") { //如果为千兆光口 速率双工配置只有千兆选项与自协商
                    document.getElementById("port"+(i+1)).innerHTML = "C"+(i+1);
                    document.getElementById("portId"+(i+1)).innerHTML = "C"+(i+1);
                }
                document.getElementById("port"+(i+1)).innerHTML = "C"+(i+1);
            
            }

            //连接状态 速率双工显示
            if(rcv[1].operational_status==1){
                $("#showTable tr:eq("+rowNum+") td:nth-child(4)").html("<b style='color: red'>"+opeObj[rcv[1].operational_status-1]+"</b>");
                $("#showTable tr:eq("+rowNum+") td:nth-child(5)").text(phyObj[rcv[2].physical_status-1]);
            }else{
                $("#showTable tr:eq("+rowNum+") td:nth-child(4)").text(opeObj[rcv[1].operational_status-1]);
                $("#showTable tr:eq("+rowNum+") td:nth-child(5)").text("--")
            }
            //管理模式
            var admMode = rcv[4].admin_mode-1;
            $("#showTable tr:eq("+rowNum+") td:nth-child(3)").text(modeObj[admMode]);
            $("[name=adminMode"+rowNum+"]")[0].selectedIndex = admMode;
            if(rcv[4].admin_mode==2){
                checkMode(1,rowNum);
            }else{
                //速率双工配置
                $("[name=physicalMode"+rowNum+"]").val(rcv[5].physical_mode);
                //流量控制
                $("[name=FlowControlMode"+rowNum+"]").val(rcv[6].flow_control_mode);
            }
        });
        //定制版增加link状态显示
        var uniqueVal = '<%=unique%>';
        var fpgaLinkObj  = ["down","up"];
        if((uniqueVal == '54-10-01-06-01-01-00-00')||(uniqueVal == '54-20-01-06-01-02-00-00')){
            var fpga_rcv = <%=luci.json.encode(fpga_rv)%>;
                //alert(JSON.stringify(fpag_rcv))
            document.getElementById('fpga_channel2').innerHTML = fpgaLinkObj[fpga_rcv.fpgaChannel2]
            var obj = document.getElementsByName('fpga_video');
            for(k=0; k<obj.length;k++){
                obj[k].innerHTML = fpgaLinkObj[fpga_rcv.fpgaVideo];
            }
            document.getElementById('fpga_channel0').innerHTML = fpgaLinkObj[fpga_rcv.fpgaChannel0]
            document.getElementById('fpga_channel1').innerHTML = fpgaLinkObj[fpga_rcv.fpgaChannel1]
        }
    });
    function checkMode(bool,num){
        if(bool){
             if( $("[name=combType"+num+"]").disabled == true){
                $("[name=physicalMode"+num+"]").attr("disabled","disabled");
                $("[name=physicalMode"+num+"]")[0].selectedIndex = 0;
             }
             $("[name=FlowControlMode"+num+"]").attr("disabled","disabled");
             $("[name=FlowControlMode"+num+"]")[0].selectedIndex = 0;
        }else{
             if( $("[name=combType"+num+"]").disabled == true){
                $("[name=physicalMode"+num+"]").removeAttr("disabled");
                $("[name=physicalMode"+num+"]")[0].selectedIndex = 1;
             }
             $("[name=FlowControlMode"+num+"]").removeAttr("disabled");
             $("[name=FlowControlMode"+num+"]")[0].selectedIndex = 2;
        }
    }
    function checkType(bool,num){
        //alert(bool);
        if(bool == 1){ //强制千兆光
             //$("[name=physicalMode"+num+"]").attr("disabled","disabled");
             //$("[name=physicalMode"+num+"]")[0].selectedIndex = 6; 
             $("[name=physicalMode"+num+"]").removeAttr("disabled");
             $("[name=physicalMode"+num+"]")[0].selectedIndex = 1;
             $("[name=physicalMode"+num+"]")[0].options[0].style.display = "none"; 
             $("[name=physicalMode"+num+"]")[0].options[1].style.display = "block"; 
             $("[name=physicalMode"+num+"]")[0].options[2].style.display = "none"; 
             $("[name=physicalMode"+num+"]")[0].options[3].style.display = "none"; 
             $("[name=physicalMode"+num+"]")[0].options[4].style.display = "none"; 
             $("[name=physicalMode"+num+"]")[0].options[5].style.display = "none"; 
             $("[name=physicalMode"+num+"]")[0].options[6].style.display = "block"; 
             $("[name=physicalMode"+num+"]")[0].options[7].style.display = "none"; 
             $("[name=physicalMode"+num+"]")[0].options[8].style.display = "none"; 
        }
        if(bool == 2){ //强制百兆光
             //$("[name=physicalMode"+num+"]").attr("disabled","disabled");
             //$("[name=physicalMode"+num+"]")[0].selectedIndex = 5; 
             $("[name=physicalMode"+num+"]").removeAttr("disabled");
             $("[name=physicalMode"+num+"]")[0].selectedIndex = 5;
             $("[name=physicalMode"+num+"]")[0].options[0].style.display = "none"; 
             $("[name=physicalMode"+num+"]")[0].options[1].style.display = "none"; 
             $("[name=physicalMode"+num+"]")[0].options[2].style.display = "none"; 
             $("[name=physicalMode"+num+"]")[0].options[3].style.display = "none"; 
             $("[name=physicalMode"+num+"]")[0].options[4].style.display = "none"; 
             $("[name=physicalMode"+num+"]")[0].options[5].style.display = "block"; 
             $("[name=physicalMode"+num+"]")[0].options[6].style.display = "none"; 
             $("[name=physicalMode"+num+"]")[0].options[7].style.display = "none"; 
             $("[name=physicalMode"+num+"]")[0].options[8].style.display = "none"; 
        }
        if(bool == 3){ //强制千兆电
             $("[name=physicalMode"+num+"]").removeAttr("disabled");
             $("[name=physicalMode"+num+"]")[0].selectedIndex = 1;
             $("[name=physicalMode"+num+"]")[0].options[0].style.display = "block"; 
             $("[name=physicalMode"+num+"]")[0].options[1].style.display = "block"; 
             $("[name=physicalMode"+num+"]")[0].options[2].style.display = "block"; 
             $("[name=physicalMode"+num+"]")[0].options[3].style.display = "block"; 
             $("[name=physicalMode"+num+"]")[0].options[4].style.display = "block"; 
             $("[name=physicalMode"+num+"]")[0].options[5].style.display = "block"; 
             $("[name=physicalMode"+num+"]")[0].options[6].style.display = "block"; 
             $("[name=physicalMode"+num+"]")[0].options[7].style.display = "none"; 
             $("[name=physicalMode"+num+"]")[0].options[8].style.display = "none"; 
        }
        if(bool == 4){ //comb复用
             $("[name=physicalMode"+num+"]").removeAttr("disabled");
             $("[name=physicalMode"+num+"]")[0].selectedIndex = 7;
             $("[name=physicalMode"+num+"]")[0].options[0].style.display = "none"; 
             $("[name=physicalMode"+num+"]")[0].options[1].style.display = "none"; 
             $("[name=physicalMode"+num+"]")[0].options[2].style.display = "none"; 
             $("[name=physicalMode"+num+"]")[0].options[3].style.display = "none"; 
             $("[name=physicalMode"+num+"]")[0].options[4].style.display = "none"; 
             $("[name=physicalMode"+num+"]")[0].options[5].style.display = "none"; 
             $("[name=physicalMode"+num+"]")[0].options[6].style.display = "none"; 
             $("[name=physicalMode"+num+"]")[0].options[7].style.display = "block"; 
             $("[name=physicalMode"+num+"]")[0].options[8].style.display = "block"; 
        }
        
    }
</script>
<div class="cbi-map"  id = "portconfig" >
    <fieldset class="cbi-section"> <!-- <fieldset> 标签可以将表单内的相关元素分组 -->
        <legend><%:Port pattern configuration%></legend> <!-- <legend> 标签为 <fieldset> 元素定义标题 -->

        <!-- <div class = "cbi-section-node"> -->
        <table id = "setTable" class = "">
            <tr>
                <th><%:Port%></th>
                <th><%:Type%></th>
                <th><%:management mode%></th>
                <th><%:rate and duplex%></th>
                <th><%:flow control%></th>
            </tr>
            <%
                for i=1,portNum do
            %>
            <tr>
                <td id = "port<%=i%>"><%=i%></td>
                <!--<td id = "type<%=i%>"><big>-</big></td> -->
                <td>
                    <select name="combType<%=i%>" >
                        <option  value="1">GX</option>
                        <option  value="2">FX</option>
                        <option  value="3">GE</option>
                        <option  value="5">FE</option>
                        <option  value="4">COMB</option>
                    </select>
                </td>
                <td>
                    <select name="adminMode<%=i%>" >
                        <option  value="1"><%:Enable%></option>
                        <option  value="2"><%:Disable%></option>
                    </select>
                </td>
                <td>
                    <select name="physicalMode<%=i%>" >
                        <option  value="0">--</option>
                        <option  value="1"><%:autonegotiation%></option>
                        <option  value="2"><%:10M-HALF%></option>
                        <option  value="3"><%:10M-FULL%></option>
                        <option  value="4"><%:100M-HALF%></option>
                        <option  value="5"><%:100M-FULL%></option>
                        <option  value="6"><%:1000M-FULL%></option>
                        <option  value="8"><%:Gigabit fiber/electric adaptive%></option>
                        <option  value="9"><%:fiber port rate adaptive%></option>
                    </select>
                </td>
                <td style="text-align:center;">
                    <select name="FlowControlMode<%=i%>" > 
                        <option  value="0">--</option>
                        <option value="1"><%:Enable%></option>
                        <option value="2"><%:Disable%></option>
                    </select>
                </td>
            </tr>
            <%
                end                               
            %>
            <tfoot>
                <tr>
                    <td colspan="5"><input type= "button" class="cbi-button cbi-input-apply" name="ok" value="<%:Apply%>" onclick = "setPort()" ></td>
                </tr>
            </tfoot>
        </table>

        <!-- </div> -->

    </fieldset>
    <div><td><%:G:GB%>&nbsp;&nbsp;&nbsp;<%:F:MB%>&nbsp;&nbsp;&nbsp;<%:X:light(fiber)%>&nbsp;&nbsp;&nbsp;<%:E:electric%>&nbsp;&nbsp;&nbsp;<%:COMB:Reuse interface%></td></div>

    <fieldset class="cbi-section"> <!-- <fieldset> 标签可以将表单内的相关元素分组 -->
        <legend><%:Port state information display%></legend> <!-- <legend> 标签为 <fieldset> 元素定义标题 -->

        <!-- <div class = "cbi-section-node"> -->
            <table class = "cbi-section-table" id = "showTable">
                <tr class = "cbi-section-table-titles">
                    <th><%:Port%></th>
                    <th><%:Type%></th>
                    <th><%:management mode%></th>
                    <th><%:connection status%></th>
                    <th><%:rate and duplex%></th>
                </tr>
                <%
                    for i=1,portNum  do
                %>
                <tr>
                    <td id = "portId<%=i%>"><%=i%></td>
                    <td id = "portType<%=i%>"><big>---</big></td>
                    <td id = "modeStatus<%=i%>"><big>---</big></td>
                    <td id = "LinkStatus<%=i%>"><big>---</big></td>
                    <td id = "physicalStatus<%=i%>"><big>---</big></td>
                    <!--             <td id = "dot1dStatus<%=i%>"><big>-</big></td> -->
                </tr>
                <%
                    end
                %>
                <%
                    if unique == "54-10-01-06-01-01-00-00" then
                %>
                <tr>
                    <td id = ""><%:SDI_IN%></td>
                    <td id = ""><big>---</big></td>
                    <td id = ""><big>---</big></td>
                    <td id = "fpga_channel2"><big>---</big></td>
                    <td id = ""><big>---</big></td>
                </tr>
                <tr>
                    <td id = ""><%:SDI_OUT%></td>
                    <td id = ""><big>---</big></td>
                    <td id = ""><big>---</big></td>
                    <td id = "fpga_video" name = "fpga_video"><big>---</big></td>
                    <td id = ""><big>---</big></td>
                </tr>
                <tr>
                    <td id = ""><%:OPT_OUT%></td>
                    <td id = ""><big>---</big></td>
                    <td id = ""><big>---</big></td>
                    <td id = "fpga_channel0"><big>---</big></td>
                    <td id = ""><big>---</big></td>
                </tr>
                <tr>
                    <td id = ""><%:OPT_IN%></td>
                    <td id = ""><big>---</big></td>
                    <td id = ""><big>---</big></td>
                    <td id = "fpga_channel1"><big>---</big></td>
                    <td id = ""><big>---</big></td>
                </tr>
                <%
                    end
                %>
                <%
                    if unique == "54-20-01-06-01-02-00-00" then
                %>
                <tr>
                    <td id = ""><%:SDI_OUT1%></td>
                    <td id = ""><big>---</big></td>
                    <td id = ""><big>---</big></td>
                    <td id = "fpga_video" name = "fpga_video"><big>---</big></td>
                    <td id = ""><big>---</big></td>
                </tr>
                <tr>
                    <td id = ""><%:SDI_OUT2%></td>
                    <td id = ""><big>---</big></td>
                    <td id = ""><big>---</big></td>
                    <td id = "fpga_video" name = "fpga_video"><big>---</big></td>
                    <td id = ""><big>---</big></td>
                </tr>
                <tr>
                    <td id = ""><%:OPT_IN%></td>
                    <td id = ""><big>---</big></td>
                    <td id = ""><big>---</big></td>
                    <td id = "fpga_channel0"><big>---</big></td>
                    <td id = ""><big>---</big></td>
                </tr>
                <tr>
                    <td id = ""><%:OPT_OUT%></td>
                    <td id = ""><big>---</big></td>
                    <td id = ""><big>---</big></td>
                    <td id = "fpga_channel1"><big>---</big></td>
                    <td id = ""><big>---</big></td>
                </tr>
                <%
                    end
                %>
            </table>

        <!-- </div> -->
    
    </fieldset>

</div>
<%+footer%>












